package cn.net.chenbao.medical.mvp;

import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;

import com.alibaba.fastjson.JSONObject;

import org.xutils.x;

import cn.net.chenbao.medical.FunctionActivity;
import cn.net.chenbao.medical.MedicalApplication;
import cn.net.chenbao.medical.R;
import cn.net.chenbao.medical.api.Api;
import cn.net.chenbao.medical.bean.Patient;
import cn.net.chenbao.medical.mvp.presenter.IFunctionP;
import cn.net.chenbao.medical.mvp.view.IFunctionV;
import cn.net.chenbao.medical.util.Consts;
import cn.net.chenbao.medical.util.DialogUtils;
import cn.net.chenbao.medical.xutils.ParamsUtils;
import cn.net.chenbao.medical.xutils.WWXCallBack;

/**
 * Created by 木头 on 2018/1/17.
 */

public class FunctionP implements IFunctionP {

    private IFunctionV mFunctionV;

    public FunctionP(IFunctionV functionV) {
        this.mFunctionV = functionV;
    }

    @Override
    public void doScanData(long scanResult) {
        //请求数据
        mFunctionV.loadStart(true);
        String url = Api.sykGet() + "?" + Consts.SESSION_ID + "=" + MedicalApplication.getInstance().getSessionId() + "&" + Consts.COMNO + "=" + scanResult;
        x.http().get(
                ParamsUtils.getParams(url),
                new WWXCallBack("SykGet") {

                    @Override
                    public void onAfterSuccessOk(JSONObject data) {
                        try {
                            mFunctionV.SykGetSuccess(JSONObject.parseObject(data.getString("Data"), Patient.class));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onAfterSuccessError(JSONObject data) {
                        super.onAfterSuccessError(data);
                    }

                    @Override
                    public void onAfterFinished() {
                        mFunctionV.loadFinish();
                    }
                });
    }

    @Override
    public void doSubmitInfo(int mode, long scanResult, long printId, int pyFlag, long scanPrintId) {
        if ((mode == Consts.MODE_KBCC || mode == Consts.MODE_APOTHECARY || mode == Consts.MODE_JC) && scanPrintId != printId) {
            mFunctionV.SubmitFail("所输批次与扫描批次不一致");
            return;
        }
        String url = null;
        String result = null;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(Consts.SESSION_ID, MedicalApplication.getInstance().getSessionId());
        jsonObject.put(Consts.COMNO, scanResult);
        switch (mode) {
            case Consts.MODE_NURSE: {
                url = Api.pyConfirm();
                result = "PyConfirm";
                break;
            }
            case Consts.MODE_KBCC:
                url = Api.outConfirm();
                result = "OutConfirm";
                jsonObject.put(Consts.PY_FLAG, pyFlag);
                break;
            case Consts.MODE_APOTHECARY: {
                url = Api.outConfirm();
                result = "OutConfirm";
                break;
            }
            case Consts.MODE_DRUG_WITHDRAWAL: {
                url = Api.backConfirm();
                result = "BackConfirm";
                break;
            }
            case Consts.MODE_HSJJ:
                url = Api.JiaoConfirm();
                result = "JiaoConfirm";
                jsonObject.put(Consts.PY_FLAG, pyFlag);
                break;
            case Consts.MODE_JC:
                url = Api.InConfirm();
                result = "InConfirm";
                jsonObject.put(Consts.PRINT_ID, printId);
                break;
        }
        x.http().post(
                ParamsUtils.getPostJsonParams(jsonObject, url),
                new WWXCallBack(result, true) {

                    @Override
                    public void onAfterSuccessOk(JSONObject data) {
                        if (data.getBoolean("Data")) {
                            mFunctionV.SubmitSuccess();
                        }
                    }

                    @Override
                    public void onAfterSuccessError(JSONObject data) {
                        String message = data.getString("Message");
                        if (!TextUtils.isEmpty(message))
                            mFunctionV.SubmitFail(message);
                    }

                    @Override
                    public void onAfterFinished() {

                    }
                });
    }
}
